home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / base / net-tool.32- / net-tool / net-tools-1.32-alpha / route.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-26  |  5.8 KB  |  232 lines

  1. /*
  2.  * route        This file contains an implementation of the command
  3.  *              that manages the IP routing table in the kernel.
  4.  *
  5.  * Version:     route 1.91 (1996-04-13)
  6.  *
  7.  * Maintainer:    Bernd 'eckes' Eckenfels, <net-tools@lina.inka.de>
  8.  *
  9.  * Author:      Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  10.  *        (derived from FvK's 'route.c     1.70    01/04/94')
  11.  *
  12.  * Modifications:
  13.  *         Johannes Stille:    for Net-2Debugged by 
  14.  *                    <johannes@titan.os.open.de>
  15.  *         Linus Torvalds:        Misc Changes
  16.  *         Alan Cox:        add the new mtu/window stuff
  17.  *         Miquel van Smoorenburg:    rt_add and rt_del
  18.  *     {1.79}    Bernd Eckenfels:    route_info
  19.  *     {1.80}    Bernd Eckenfels:    reject, metric, irtt, 1.2.x support.
  20.  *     {1.81}    Bernd Eckenfels:    reject routes need a dummy device
  21.  *960127 {1.82} Bernd Eckenfels:    'mod' and 'dyn' 'reinstate' added
  22.  *960129 {1.83}    Bernd Eckenfels:    resolve and getsock now in lib/, 
  23.  *                    REJECT displays '-' as gatway.
  24.  *960202 {1.84} Bernd Eckenfels:    net-features support added
  25.  *960203 {1.85} Bernd Eckenfels:    "#ifdef' in '#if' for net-features
  26.  *                    -A  (aftrans) support, get_longopts
  27.  *960206 {1.86} Bernd Eckenfels:    route_init();
  28.  *960218 {1.87} Bernd Eckenfels:    netinet/in.h added
  29.  *960221 {1.88}    Bernd Eckenfels:    aftrans_dfl support
  30.  *960222 {1.90} Bernd Eckenfels:    moved all AF specific code to lib/.
  31.  *960413 {1.91} Bernd Eckenfels:    new RTACTION support+FLAG_CACHE/FIB
  32.  *960426 {1.92} Bernd Eckenfels:    FLAG_SYM/-N support
  33.  *
  34.  */
  35. #include <sys/types.h>
  36. #include <sys/ioctl.h>
  37. #include <sys/socket.h>
  38. #include <net/if.h>
  39. #include <netinet/in.h>
  40. #include <netdb.h>
  41. #include <netinet/in.h>
  42. #include <arpa/nameser.h>
  43. #include <resolv.h>
  44. #include <linux/param.h>
  45. #include <stdio.h>
  46. #include <errno.h>
  47. #include <fcntl.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include <getopt.h>
  51. #include <unistd.h>
  52. #include <ctype.h>
  53. #include <linux/route.h>
  54. #include "net-support.h"
  55. #include "config.h"
  56. #include "net-locale.h"
  57. #include "pathnames.h"
  58. #include "version.h"
  59.  
  60. #define DFLT_AF "inet"
  61.  
  62. #define FEATURE_ROUTE
  63. #include "lib/net-features.h" /* needs some of the system includes above! */
  64.  
  65. char    *Release = RELEASE,
  66.     *Version = "route 1.92 (1996-04-26)";
  67.  
  68. int             opt_n  = 0;    /* numerical output flag    */
  69. int             opt_v  = 0;    /* debugging output flag    */
  70. int        opt_e  = 1;    /* 1,2,3=type of routetable    */
  71. int        opt_fc = 0;    /* routing cache/FIB */
  72. int        opt_h  = 0;    /* help selected        */
  73. struct aftype   *ap;        /* current address family    */
  74.  
  75. static void usage(void)
  76. {
  77.     fprintf(stderr, NLS_CATGETS(catfd, routeSet, route_usage1,
  78.                 "Usage: route [-nNvee] [-FC] [Address_families]  List kernel routing tables\n"));
  79.     fprintf(stderr, NLS_CATGETS(catfd, routeSet, route_usage2,
  80.                 "       route {-V|--version}                  Display command version and exit.\n"));
  81.     fprintf(stderr, NLS_CATGETS(catfd, routeSet, route_usage3,
  82.                 "       route {-h|--help} [Address_family]    Usage Syntax for specified AF.\n"));
  83.     fprintf(stderr, NLS_CATGETS(catfd, routeSet, route_usage4,
  84.                 "       route [-v] [-FC] {add|del|flush} ...  Modify routing table for AF.\n\n"));
  85.     fprintf(stderr, NLS_CATGETS(catfd, routeSet, route_usage5,
  86.                 "  Address_families: inet,ddp,ipx,netrom,ax25\n"));
  87.     fprintf(stderr, NLS_CATGETS(catfd, routeSet, route_usage6,
  88.                 "        specify AF: -A af1,af2..  or  --af1 --af2  or  af_route\n"));
  89.     NLS_CATCLOSE(catfd)
  90.     exit(E_USAGE);
  91. }
  92.  
  93.  
  94. static void version(void)
  95. {
  96.     fprintf(stderr,"%s\n%s\n%s\n",Release,Version,Features);
  97.     NLS_CATCLOSE(catfd)
  98.     exit(E_VERSION);
  99. }
  100.  
  101.  
  102. int main(int argc, char **argv)
  103. {
  104.     int i,lop,what=0;
  105.     struct option longopts[]=
  106.     {
  107.         AFTRANS_OPTS,
  108.         {"extend",    0,    0,    'e'},
  109.         {"verbose",    0,    0,    'v'},
  110.         {"version",    0,    0,    'V'},
  111.         {"numeric",    0,    0,    'n'},
  112.         {"symbolic",    0,    0,    'N'},
  113.         {"protocol",    1,    0,    'A'},
  114.         {"cache",    0,    0,    'C'},
  115.         {"fib",        0,    0,    'F'},
  116.         {"help",    0,    0,    'h'},
  117.         {NULL,        0,    0,    0}
  118.     };    
  119.     char **tmp;
  120.     char *progname;
  121.     int options;
  122.  
  123. #if NLS
  124.     setlocale (LC_MESSAGES, "");
  125.     catfd = catopen ("nettools", MCLoadBySet);
  126. #endif
  127.  
  128.     getroute_init();             /* Set up AF routing support */
  129.     setroute_init();
  130.     afname[0] = '\0';
  131.     progname=argv[0];
  132.     
  133.     /* getopts and -net wont work :-/ */
  134.     for(tmp=argv;*tmp;tmp++) {
  135.         if (!strcmp(*tmp, "-net"))
  136.             strcpy(*tmp, "#net");
  137.         else if (!strcmp(*tmp, "-host"))
  138.             strcpy(*tmp, "#host");
  139.     }
  140.     
  141.     /* Fetch the command-line arguments. */
  142.     while ((i = getopt_long(argc, argv, "A:eCFhnNVv?", longopts, &lop)) != EOF) switch(i) {
  143.         case -1:
  144.             break;
  145.         case 'n':
  146.             opt_n |= FLAG_NUM;
  147.             break;
  148.         case 'N':
  149.             opt_n |= FLAG_SYM;
  150.             break;
  151.         case 'v':
  152.             opt_v |= FLAG_VERBOSE;
  153.             break;
  154.         case 'e':
  155.             opt_e++;
  156.             break;
  157.         case 1:
  158.             if (lop < 0 || lop >= AFTRANS_CNT) {
  159.                 EINTERN("route.c","longopts 1 range");
  160.                 break;
  161.             }
  162.             if ((i = aftrans_opt(longopts[lop].name))) {
  163.                 NLS_CATCLOSE(catfd)
  164.                 exit(i);
  165.             }
  166.             break;
  167.         case 'C':
  168.             opt_fc |= FLAG_CACHE;
  169.             break;
  170.         case 'F':
  171.             opt_fc |= FLAG_FIB;
  172.             break;
  173.         case 'A':
  174.             if ((i = aftrans_opt(optarg))) {
  175.                 NLS_CATCLOSE(catfd)
  176.                 exit(i);
  177.             }
  178.             break;
  179.         case 'V':
  180.             version();
  181.         case 'h':
  182.         case '?':
  183.             opt_h++;
  184.             break;
  185.         default:
  186.             usage();
  187.     }
  188.     
  189.     argv += optind;
  190.     argc -= optind;
  191.     
  192.     if (opt_h) {
  193.         if (!afname[0])
  194.             usage();
  195.         else
  196.             what = RTACTION_HELP;
  197.     } else {    
  198.         if (!afname[0])
  199.             /* this will initialise afname[] */
  200.             aftrans_def("route", progname, DFLT_AF);
  201.  
  202.         /* Do we have to show the contents of the routing table? */
  203.         if (*argv == NULL) {
  204.             what = RTACTION_SHOW;
  205.         } else {
  206.             if (!strcmp(*argv, "add"))
  207.                 what = RTACTION_ADD;
  208.             else if (!strcmp(*argv, "del") || !strcmp(*argv, "delete"))
  209.                 what = RTACTION_DEL;
  210.             else if (!strcmp(*argv,"flush"))
  211.                 what = RTACTION_FLUSH;
  212.             else 
  213.                 usage();
  214.         }
  215.     }
  216.  
  217.     options = (opt_e & FLAG_EXT) | opt_n | opt_fc | opt_v;
  218.     if (!opt_fc)
  219.         options |= FLAG_FIB;
  220.  
  221.     if (what == RTACTION_SHOW)
  222.         i = route_info(afname,options);
  223.     else
  224.         i = route_edit(what,afname,options,++argv);
  225.  
  226.     if (i == E_OPTERR)
  227.         usage();
  228.  
  229.     NLS_CATCLOSE(catfd)
  230.     return (i);
  231. }
  232.